import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/demo/page/footer_underscroll.dart';
import 'package:im_app/im/page/view/LoadingView.dart';
import 'package:refresh_loadmore/refresh_loadmore.dart';

import 'LoadMoreCubit.dart';

class LoadMoreCubitPage extends StatefulWidget {
  LoadMoreCubitPage({Key? key, required this.title}) : super(key: key);

  final String title;

  @override
  _LoadMoreCubitPageState createState() => _LoadMoreCubitPageState();
}

class _LoadMoreCubitPageState extends State<LoadMoreCubitPage> {
  bool isLastPage = false;

  ScrollController? _scrollController;

  @override
  void initState() {
    super.initState();

    _scrollController = ScrollController();

    var loadMoreCubit = context.read<LoadMoreCubit>();
    loadMoreCubit.add(LoadMoreInitEvent());
  }

  @override
  Widget build(BuildContext context) {
    var loadMoreCubit = context.watch<LoadMoreCubit>();
    var dataList = loadMoreCubit.state.dataList;
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: RefreshLoadmore(
            onRefresh: () async {
              await Future.delayed(Duration(milliseconds: 100));
              loadMoreCubit.add(LoadMoreLoadOldsEvent());
            },
            onLoadmore: () async {
              await Future.delayed(Duration(milliseconds: 100));
              loadMoreCubit.add(LoadMoreLoadNewsEvent());
            },
            isLastPage: isLastPage,
            child: Column(
              children: dataList
                  .map(
                    (e) => ListTile(
                      title: Item(title: e),
                      trailing: Icon(Icons.accessibility_new),
                    ),
                  )
                  .toList(),
            )));
  }

  @override
  void dispose() {
    super.dispose();
    _scrollController?.dispose();
  }
}
